package gao.xiaolei.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;
import gao.xiaolei.excepition.MyExcepition;

public class FileManage {

	public static void fileUpload(String filePath,MultipartFile file) {
		File f = new File(filePath);
		if(!f.exists())
			f.mkdir();
		byte[] fileByte=null;
		try {
			fileByte =file.getBytes();
			FileCopyUtils.copy(fileByte, f);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static byte[] fileDownload(String filePath) throws MyExcepition {
		byte[] fileByte=null;
		File f = new File(filePath);
		if (!f.exists())// 如果文件不存在
			throw new MyExcepition("文件不存在!");
		else {// 将文件二进制数据返回
			InputStream fis = null;
			try {
				fis = new FileInputStream(filePath);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("fileDownload----end");
			try {
				fileByte=FileCopyUtils.copyToByteArray(fis);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return fileByte;
	}
}
